<template>
  <el-dialog title="修改密码" :close-on-click-modal="false" :visible.sync="visible">
    <el-form
      size="small"
      :model="dataForm"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="dataForm.password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="password">
        <el-input type="password" v-model="dataForm.password" placeholder="密码"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        userId: 0,
        password: ''
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.userId = id || 0
      this.visible = true
    },
    // 表单提交
    async dataFormSubmit() {
      let valid = await this.$refs['dataForm'].validate()
      if (!valid) return

      let res = await this.$http({
        url: `/user/member/${!this.dataForm.userId ? 'save' : 'update'}`,
        method: 'post',
        data: {
          userId: this.dataForm.userId || undefined,
          password: this.dataForm.password
        }
      })

      if (res && res.code === 200) {
        this.$message({
          message: '操作成功',
          type: 'success',
          duration: 1500
        })
        this.visible = false
        this.$emit('refreshDataList')
      } else {
        this.$message.error(res.msg)
      }
    }
  }
}
</script>
